package com.example.dingtalk;

import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import org.apache.commons.codec.binary.Base64;
import org.junit.Before;
import org.junit.Test;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.Arrays;

public class DingTalkTest {
    static DingTalkClient client = null;

    static OapiRobotSendRequest request = new OapiRobotSendRequest();

    @Before
    public void init() {
        try {
            Long timestamp = System.currentTimeMillis();
            String secret = "SEC688e8e3851b8df73d690c975a6ad2d8f7a0a129bcfec1c97983e222aa2b31292";
            String stringToSign = timestamp + "\n" + secret;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
            client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=03dcf61dca4ce57b648ed62d89d01bc18b136f6a3cc0aa0dace998b71976c582&timestamp=" + timestamp + "&sign=" + sign);
        } catch (Exception e) {

        }
    }

    @Test
    public void testTxt() throws ApiException {
        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent("https://blackholesitl.cqfmbank.com/risk/weekReport/");
        request.setText(text);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        at.setAtMobiles(Arrays.asList("132xxxxxxxx"));
        at.setIsAtAll(false);
        at.setAtUserIds(Arrays.asList("109929", "32099"));
        request.setAt(at);
        execute(request);
    }

    @Test
    public void testLink() throws ApiException {
        request.setMsgtype("link");
        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
        link.setMessageUrl("https://blackholesitl.cqfmbank.com/risk/weekReport/");
        link.setPicUrl("");
        link.setTitle("【周报】风险策略模型优化交付周报");
        link.setText("由于消息中心不支持这样的链接发送，还是给大家看看效果");
        request.setLink(link);
        execute(request);

    }

    public void testMarkDown() throws ApiException {
        request.setMsgtype("markdown");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("重庆天气");
        markdown.setText("#### 杭州天气 @156xxxx8827\n" +
                "> 9度，西北风1级，空气良89，相对温度73%\n\n" +
                "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n" +
                "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
        request.setMarkdown(markdown);
        execute(request);

    }


    @Test
    public void testActionCard() throws ApiException {
        request.setMsgtype("actionCard");
        OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();
        actioncard.setTitle("风控项目管理系统推送功能Demo");
        actioncard.setText("![screenshot](https://www.hackcloud.cn/wp-content/uploads/2021/12/8b54a01487951bc.png)" +
                "#### 下一步消息推送将展示数据图表 " +
                "RISK PROJECT 的设计正从原来满满的科技感走向生活化，而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");
        actioncard.setBtnOrientation("0");
        actioncard.setSingleTitle("阅读全文");
        actioncard.setSingleURL("https://www.hackcloud.cn/java-684.html");
        request.setActionCard(actioncard);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        at.setAtMobiles(Arrays.asList("18623694606"));
        at.setIsAtAll(true);
        request.setAt(at);
        execute(request);
    }

    public static void execute(OapiRobotSendRequest request) throws ApiException {
        OapiRobotSendResponse response = client.execute(request);
        System.out.println(JSON.toJSONString(response));
    }
}
